package com.ccepc.algmanage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccepc.algmanage.mapper.DeployInfoMapper;
import com.ccepc.algmanage.service.AlgorithmConfigService;
import com.ccepc.algmanage.service.DeployInfoService;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.demo.api.entity.AlgorithmConfig;
import com.ccepc.demo.api.entity.DeployInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-09 10:52:14
 */
@Service
public class DeployInfoServiceImpl extends ServiceImpl<DeployInfoMapper, DeployInfo> implements DeployInfoService {
    @Resource
    private AlgorithmConfigService algorithmConfigService;

    @Transactional
    @Override
    public Object removeBatch(List<DeployInfo> deployInfoList) {
        deployInfoList.forEach(deploy -> {
            algorithmConfigService.remove(
                    new LambdaQueryWrapper<AlgorithmConfig>()
                            .eq(AlgorithmConfig::getDeployId, deploy.getId())
            );
            this.removeById(deploy.getId());
        });
        return true;
    }

    @Override
    public R saveOrUpdateDepl(DeployInfo deployInfo) {
        int count = this.count(new LambdaQueryWrapper<DeployInfo>()
                .eq(DeployInfo::getAlgorimId, deployInfo.getAlgorimId())
                .eq(DeployInfo::getServerId, deployInfo.getServerId()));
        if (count == 0 || deployInfo.getId() != null) {
            return R.ok(CommonUtils.saveOrUpdate(deployInfo, this));
        } else {
            return R.failed("该服务器已部署该算法");
        }
    }

    @Override
    public R alloCount(Integer algorimId) {
        JSONObject jsonObject = new JSONObject();
        List<DeployInfo> deployInfos = this.list(new LambdaQueryWrapper<DeployInfo>()
                .eq(DeployInfo::getAlgorimId, algorimId));
        // 几路算法
        jsonObject.put("algorithmRoutes", deployInfos.size());
        AtomicInteger count = new AtomicInteger();
        deployInfos.forEach(deployInfo -> {
            List<AlgorithmConfig> algorithmConfigs = algorithmConfigService.list(new LambdaQueryWrapper<AlgorithmConfig>()
                    .eq(AlgorithmConfig::getDeployId, deployInfo.getId()));
            algorithmConfigs.forEach(algorithmConfig -> {
                if (algorithmConfig.getCameraIds() != null){
                    String[] split = algorithmConfig.getCameraIds().split(",");
                    count.addAndGet(split.length);
                }
            });
        });
        // 当前配置
        jsonObject.put("currentAllocated", count);
        return R.ok(jsonObject);
    }

}
